#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：PythonProjects
@File    ：spilt_char.py
@IDE     ：PyCharm
@Author  ：pipibao
@Date    ：2021/7/6 下午2:41
'''

# 步长2像素，通过搜索全０分割
import numpy as np
import cv2 as cv
import op
import os


def wt(x1, x2, img, radix, name):
    cnt = [[x1, 0], [x1, len(img) - 1], [x2, 0], [x2, len(img) - 1]]
    flag, res = op.cut_image(img, cnt)
    res = op.get_img_reserve(res)
    res = cv.resize(res, (32, 32))
    # cv.imshow("cut", res)
    # cv.waitKey(0)
    if not os.path.exists("Image/car_char/" + name):
        os.mkdir("Image/car_char/" + name)
        print("生成目录：" + "Image/car_char/" + name)
    cv.imwrite("Image/car_char/" + name + "/" + str(radix) + ".jpg", res)


def spilt_char(name):
    img = cv.imread("Image/car_id/" + name)
    img = op.image_binarization(img)
    lens = len(img[0])
    pack = [0 for e in range(lens)]  # 压缩图片
    for i in range(lens):
        for j in range(len(img)):
            if img[j][i] == 0:
                pack[i] = pack[i] + 1
        print(pack[i], end=" ")
    x1 = x2 = 0
    i = 0
    radix = 1
    while i < lens - 3:
        i = i + 2
        if pack[i] + pack[i + 1] < 5 and i - x1 > lens / 10:
            x2 = i
            wt(x1, x2, img, radix, name)
            x1 = i
            radix = radix + 1

    if radix <= 7:
        wt(x1, x2, img, radix, name)


if __name__ == '__main__':
    spilt_char("1.jpeg")
